package com.hmy.infrastructure.service;

import cn.hutool.core.lang.Assert;
import cn.hutool.json.JSONUtil;
import com.fasc.open.api.bean.base.BaseRes;
import com.fasc.open.api.bean.common.OpenId;
import com.fasc.open.api.v5_1.req.signtask.GetOwnerSignTaskListReq;
import com.fasc.open.api.v5_1.req.signtask.SignTaskBaseReq;
import com.fasc.open.api.v5_1.req.template.GetSignTemplateListReq;
import com.fasc.open.api.v5_1.req.template.SignTemplateDetailReq;
import com.fasc.open.api.v5_1.req.template.SignTemplateListFilterInfo;
import com.fasc.open.api.v5_1.res.signtask.GetSignTaskListRes;
import com.fasc.open.api.v5_1.res.signtask.SignTaskDetailRes;
import com.fasc.open.api.v5_1.res.signtask.SignTaskInfo;
import com.fasc.open.api.v5_1.res.template.SignTemplateDetailRes;
import com.fasc.open.api.v5_1.res.template.SignTemplateListRes;
import com.hmy.infrastructure.service.signature.impl.FadadaBaseSignatureService;
import lombok.Data;

import java.util.List;

/**
 * @Description 法大大通过模板窗前签署任务单元测试
 * @Author seven.qi
 * @Date 2023/11/23 19:52
 * @Version 1.0
 */
@Data
public class FadadaContractService {


    private FadadaBaseSignatureService fadadaBaseSignatureService;

    private static OpenId openId;

    private static String clientCorpId;

    private static String openCorpId;

    private String token;

    public FadadaContractService() {

    }

    public FadadaContractService(FadadaBaseSignatureService fadadaBaseSignatureService) {
        this.fadadaBaseSignatureService = fadadaBaseSignatureService;
    }

    public void setup() throws Exception {
        openCorpId = "a8c51bf9940544468763d9d055f83dee";
        openId = OpenId.getInstance("corp", openCorpId);
        token = fadadaBaseSignatureService.getServiceClient().getAccessToken().getData().getAccessToken();
    }

    public String token() throws Exception {
        return fadadaBaseSignatureService.getServiceClient().getAccessToken().getData().getAccessToken();
    }


    /**
     * 获取签署模板列表
     * @throws Exception
     */
    public SignTemplateListRes getSignTemplate() throws Exception{
        GetSignTemplateListReq getSignTemplateListReq = new GetSignTemplateListReq();
        getSignTemplateListReq.setOwnerId(openId);
        getSignTemplateListReq.setAccessToken(token);
        BaseRes<SignTemplateListRes> signTemplateList = fadadaBaseSignatureService.getTemplateClient().getSignTemplateList(getSignTemplateListReq);
        System.out.println(JSONUtil.toJsonStr(signTemplateList));
        Assert.notNull(signTemplateList);
        Assert.notNull(signTemplateList.getData());
        return signTemplateList.getData();
    }

    /**
     * 条件获取签署模板列表
     * @throws Exception
     */
    public SignTemplateListRes getSignTemplate(SignTemplateListFilterInfo filterInfo) throws Exception{
        GetSignTemplateListReq getSignTemplateListReq = new GetSignTemplateListReq();
        getSignTemplateListReq.setOwnerId(openId);
        getSignTemplateListReq.setAccessToken(token);
        BaseRes<SignTemplateListRes> signTemplateList = fadadaBaseSignatureService.getTemplateClient().getSignTemplateList(getSignTemplateListReq);
        System.out.println(JSONUtil.toJsonStr(signTemplateList));
        Assert.notNull(signTemplateList);
        Assert.notNull(signTemplateList.getData());
        return signTemplateList.getData();
    }

    /**
     * 根据模板id获取签署模板详情
     *
     * @throws Exception
     */
    public SignTemplateDetailRes getSignTemplateDetail(String signTemplateId) throws Exception {
        SignTemplateDetailReq signTemplateDetailReq = new SignTemplateDetailReq();
        signTemplateDetailReq.setSignTemplateId(signTemplateId);
        signTemplateDetailReq.setAccessToken(token);
        // ownerId必填，不然会找不到对应的数据
        signTemplateDetailReq.setOwnerId(openId);
        BaseRes<SignTemplateDetailRes> signTemplateDetail = fadadaBaseSignatureService.getTemplateClient().getSignTemplateDetail(signTemplateDetailReq);
        System.out.println(JSONUtil.toJsonStr(signTemplateDetail));
        Assert.notNull(signTemplateDetail);
        return signTemplateDetail.getData();
    }

    /**
     * 获取签署任务列表
     *
     * @throws Exception
     */
    public List<SignTaskInfo> getSignTaskList() throws Exception {
        GetOwnerSignTaskListReq getOwnerSignTaskListReq = new GetOwnerSignTaskListReq();
        getOwnerSignTaskListReq.setOwnerId(openId);
        getOwnerSignTaskListReq.setAccessToken(token);
        BaseRes<GetSignTaskListRes> ownerList = fadadaBaseSignatureService.getSignTaskClient().getOwnerList(getOwnerSignTaskListReq);
        System.out.println(JSONUtil.toJsonStr(ownerList));
        Assert.notNull(ownerList.getData());
        Assert.notNull(ownerList.getData().getSignTasks());
        return ownerList.getData().getSignTasks();
    }

    /**
     * 获取签署任务详情
     *
     * @throws Exception
     */
    public SignTaskDetailRes getSignTaskDetail(String taskId) throws Exception {
        SignTaskBaseReq signTaskBaseReq = new SignTaskBaseReq();
        signTaskBaseReq.setAccessToken(token);
        signTaskBaseReq.setSignTaskId(taskId);
        BaseRes<SignTaskDetailRes> detail = fadadaBaseSignatureService.getSignTaskClient().getDetail(signTaskBaseReq);
        System.out.println(JSONUtil.toJsonStr(detail));
        Assert.notNull(detail.getData());
        return detail.getData();
    }

    public SignTemplateDetailRes getSignTemplateDetailByName(String name) throws Exception {
        SignTemplateListFilterInfo signTemplateListFilterInfo = new SignTemplateListFilterInfo();
        signTemplateListFilterInfo.setSignTemplateName(name);
        SignTemplateListRes signTemplate = getSignTemplate(signTemplateListFilterInfo);
        Assert.notNull(signTemplate);
        SignTemplateDetailRes signTemplateDetail = getSignTemplateDetail(signTemplate.getSignTemplates().get(0).getSignTemplateId());
        Assert.notNull(signTemplateDetail);
        return signTemplateDetail;
    }

    public SignTemplateListRes getTemplateFile() throws Exception{
        GetSignTemplateListReq getSignTemplateListReq = new GetSignTemplateListReq();
        getSignTemplateListReq.setOwnerId(openId);
        getSignTemplateListReq.setAccessToken(token);
        SignTemplateListFilterInfo signTemplateListFilterInfo = new SignTemplateListFilterInfo();
        signTemplateListFilterInfo.setSignTemplateName("设备进场单-测试模版1");

        getSignTemplateListReq.setListFilter(signTemplateListFilterInfo);
        BaseRes<SignTemplateListRes> signTemplateList = fadadaBaseSignatureService.getTemplateClient().getSignTemplateList(getSignTemplateListReq);
        Assert.notNull(signTemplateList);
        Assert.notNull(signTemplateList.getData());
        System.out.println(JSONUtil.toJsonPrettyStr(signTemplateList.getData()));
        return signTemplateList.getData();
    }

    /**
     * 根据模板id获取签署文件详情
     * @throws Exception
     */
    public SignTemplateDetailRes getTemplateFileDetail(String templateId) throws Exception {
        SignTemplateDetailReq signTemplateDetailReq = new SignTemplateDetailReq();
        signTemplateDetailReq.setSignTemplateId(templateId);
        signTemplateDetailReq.setAccessToken(token);
        // ownerId必填，不然会找不到对应的数据
        signTemplateDetailReq.setOwnerId(openId);
        BaseRes<SignTemplateDetailRes> signTemplateDetail = fadadaBaseSignatureService.getTemplateClient().getSignTemplateDetail(signTemplateDetailReq);
        Assert.notNull(signTemplateDetail);
        Assert.notNull(signTemplateDetail.getData());
        System.out.println(JSONUtil.toJsonPrettyStr(signTemplateDetail.getData()));
        return signTemplateDetail.getData();
    }


}
